package routes

import (
	v1 "ginblog/api/v1"
	"ginblog/middleware"
	"ginblog/utils"
	"github.com/gin-gonic/gin"
)

func InitRouter() {
	middleware.InitLogger()
	middleware.SugarLogger.Info("InitLogger success")
	gin.SetMode(utils.AppMode)
	g := gin.New()
	g.Use(gin.Recovery())
	//g.Use(middleware.Logger())
	admin := g.Group("/admin/v1")
	//  Bearer Token: 管理员权限验证方式
	//  Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImN4azEiLCJleHAiOjE3NTc0NTc4MjAsImlzcyI6IkdpbkJsb2cifQ.w5pvgvDWSZEraKvOU2sqtPgh0SWTQ5Oh6R7blejvbB0
	admin.Use(middleware.JwtTwoToken())
	{
		//用户操作
		admin.PUT("user/:id", v1.UpdateUser)
		admin.DELETE("user/:id", v1.DeleteUser)

		//文章操作
		admin.DELETE("article/:id", v1.DeleteArt)

		//分类操作
		admin.PUT("category/:id", v1.UpdateCate)
		admin.DELETE("category/:id", v1.DeleteCate)

		//上传文件
		admin.POST("upload", v1.UpLoad)
	}

	Router := g.Group("/api/v1")
	{
		//用户接口路由
		Router.POST("user/add", v1.AddUser)
		Router.GET("users", v1.FindUsers)

		//文章接口路由
		Router.POST("article/add", v1.AddArticle)
		Router.GET("article", v1.FindArt)              //查询文章列表
		Router.GET("article/info/:id", v1.FindArtInfo) //查询指定文章
		Router.PUT("article/:id", v1.UpdateArt)
		Router.GET("article/:cid", v1.FindCateArticles) //查询指定分类下的全部文章

		//分类接口路由
		Router.POST("category/add", v1.AddCategory)
		Router.GET("category", v1.FindCate)

		Router.POST("login", v1.Login)
	}

	if err := g.Run(utils.HttpPort); err != nil {
		middleware.SugarLogger.Fatal(err)
	} else {
		middleware.SugarLogger.Info("%s run success", utils.HttpPort)
	}
}
